package cn.jason.basic.controller;


/**
 * 图形验证码  短信验证码
 */

import cn.jason.basic.dto.RegisterDto;
import cn.jason.basic.exception.BusinessException;
import cn.jason.basic.service.IveridyCodeService;
import cn.jason.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class ImageVerifyCodeController {

    @Autowired
    private IveridyCodeService iveridyCodeService;

    /**
     * 短信验证码
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto ){
        try {
            iveridyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {//我们自己的业务异常，我们需要自己处理
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }

    /**
     * 图形验证码
     * @param key
     * @return
     */

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult image(@PathVariable("imageCodeKey") String key){
        try {
            String base64 = iveridyCodeService.ImageCode(key);
            return AjaxResult.me().setResultObj(base64);
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存失败"+e.getMessage());
        }
    }
}
